package com.roc.template.mongo.dao.impl;

import com.roc.template.activiti.dto.HistoryProcess;
import com.roc.template.activiti.dto.LeaveTask;
import com.roc.template.base.PageBean;
import com.roc.template.mongo.base.BaseDaoImpl;
import com.roc.template.mongo.dao.LeaveDao;
import com.roc.template.mongo.model.LeaveApply;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName LeavDaoImpl
 * @Description: TODO
 * @Author Administrator
 * @Date 2020/8/25 11 15
 * @Version V1.0
 **/

@Repository
public class LeavDaoImpl extends BaseDaoImpl<LeaveApply> implements LeaveDao {

    @Resource
    RuntimeService runtimeservice;
    @Resource
    TaskService taskservice;

    @Resource
    private HistoryService historyService;

    public ProcessInstance startWorkflow(LeaveApply apply, String userid, Map<String, Object> variables) {
        apply.setApply_time(new Date().toString());
        apply.setUser_id(userid);
        apply = save(apply);
        String businesskey=String.valueOf(apply.getId());//使用leaveapply表的主键作为businesskey,连接业务数据和流程数据
        ProcessInstance instance=runtimeservice.startProcessInstanceByKey("leave",businesskey,variables);
        String instanceid=instance.getId();
        apply.setProcess_instance_id(instanceid);
        update(apply.getId(),apply);
        return instance;
    }

    public PageBean<LeaveTask> getMyTaskList(String userid, int pageNum, int pageSize) {
        int total = getMyAllTask(userid);
        List<LeaveTask> leaveTasks = new ArrayList<LeaveTask>();
        List<Task> tasks=taskservice.createTaskQuery().taskAssignee(userid).listPage(pageNum, pageSize);
        for(Task task:tasks){
            String instanceid=task.getProcessInstanceId();
            ProcessInstance ins=runtimeservice.createProcessInstanceQuery().processInstanceId(instanceid).singleResult();
            String businesskey=ins.getBusinessKey();
            LeaveApply a=queryById(businesskey);
            a.setTask(task);
            LeaveTask leaveTask = new LeaveTask();
            leaveTask.setApply_time(a.getApply_time());
            leaveTask.setUser_id(a.getUser_id());
            leaveTask.setEnd_time(a.getEnd_time());
            leaveTask.setId(a.getId());
            leaveTask.setLeave_type(a.getLeave_type());
            leaveTask.setProcess_instance_id(a.getProcess_instance_id());
            leaveTask.setProcessdefid(a.getTask().getProcessDefinitionId());
            leaveTask.setReason(a.getReason());
            leaveTask.setStart_time(a.getStart_time());
            leaveTask.setTaskcreatetime(a.getTask().getCreateTime());
            leaveTask.setTaskid(a.getTask().getId());
            leaveTask.setTaskname(a.getTask().getName());
            leaveTasks.add(leaveTask);
        }
        PageBean<LeaveTask> pageBean = new PageBean<>(leaveTasks, pageNum, pageSize, total);
        return pageBean;
    }

    public int getMyAllTask(String userid) {
        List<Task> tasks=taskservice.createTaskQuery().taskAssignee(userid).list();
        return tasks.size();
    }

    @Override
    public PageBean<HistoryProcess> getHistory(String openId, int pageNum, int pageSize) {
        int total = getAllHistory(openId);
        List<HistoricProcessInstance> leave = historyService.createHistoricProcessInstanceQuery().processDefinitionKey("leave")
                .listPage(pageNum, pageSize);
        List<HistoryProcess> list = new ArrayList<HistoryProcess>();
        for (HistoricProcessInstance history : leave) {
            HistoryProcess his = new HistoryProcess();
            String bussinesskey = history.getBusinessKey();
            LeaveApply apply = queryById(bussinesskey);
            his.setLeaveapply(apply);
            his.setBusinessKey(bussinesskey);
            his.setProcessDefinitionId(history.getProcessDefinitionId());
            list.add(his);
        }
        PageBean<HistoryProcess> pageBean = new PageBean<>(list, pageNum, pageSize, total);
        return pageBean;
    }

    private int getAllHistory(String openId) {
        List<HistoricProcessInstance> leave = historyService.createHistoricProcessInstanceQuery().processDefinitionKey("leave").list();
        return leave.size();
    }

}
